n = int(input())
board = [[' ' for _ in range(n)] for _ in range(n)]
for k in range(n):
    tc = input().split()
    if 'A' in tc:
        start = (k, tc.index('A'))
    if 'B' in tc:
        end = (k, tc.index('B'))
    board[k] = tc
import heapq

heap = [(0, start)]


def main():
    while heap:
        distance, curr = heapq.heappop(heap)
        curr_i, curr_j = curr
        for nx, ny in [(curr_i + 1, curr_j), (curr_i - 1, curr_j), (curr_i, curr_j + 1), (curr_i, curr_j - 1)]:
            if nx < 0 or nx >= n: continue
            if ny < 0 or ny >= n: continue
            if board[curr_i][curr_j] == board[nx][ny]: continue
            if (nx, ny) == end:
                print(distance + 1)
                return
            heapq.heappush(heap, [distance + 1, (nx, ny)])


main()
